﻿using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Globalization;

namespace HHH.Base
{
    public sealed class EncryptHelper
    {
        private readonly static string _DefaultKey = "fservjgys";

        public static string DecodeBase64(string source)
        {
            if (string.IsNullOrEmpty(source))
                return string.Empty;
            byte[] bytes = Convert.FromBase64String(source);
            return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
        }

        public static string EncodeBase64(string source)
        {
            if (string.IsNullOrEmpty(source))
                return string.Empty;
            byte[] bytes = Encoding.UTF8.GetBytes(source);
            return Convert.ToBase64String(bytes, 0, bytes.Length);
        }

        public static string EncodeBase64(string code_type, string code)
        {
            if (string.IsNullOrEmpty(code))
                return string.Empty;

            string encode = "";
            byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = code;
            }
            return encode;
        }

        public static string DecodeBase64(string code_type, string code)
        {
            if (string.IsNullOrEmpty(code))
                return string.Empty;

            string decode = "";
            byte[] bytes = Convert.FromBase64String(code);
            try
            {
                decode = Encoding.GetEncoding(code_type).GetString(bytes);
            }
            catch
            {
                decode = code;
            }
            return decode;
        }

        public static string EncodeBase64(Encoding codeType, string code)
        {
            if (string.IsNullOrEmpty(code))
                return string.Empty;

            string encode = "";
            byte[] bytes = codeType.GetBytes(code);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = code;
            }
            return encode;
        }

        public static string DecodeBase64(Encoding codeType, string code)
        {
            if (string.IsNullOrEmpty(code))
                return string.Empty;

            string decode = "";
            byte[] bytes = Convert.FromBase64String(code);
            try
            {
                decode = codeType.GetString(bytes);
            }
            catch
            {
                decode = code;
            }
            return decode;
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string DesEncryString(string source)
        {
            return DesEncryString(source, _DefaultKey);
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DesEncryString(string source, string key)
        {
            return TripleDES.EncryptWithKey(source, key);

        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string DesDecryString(string source)
        {
            return DesDecryString(source, _DefaultKey);
        }


        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DesDecryString(string source, string key)
        {
            return TripleDES.DecryptWithKey(source, key);
        }


        /// <summary>
        /// 从原始数据加密成密码，再加密为UTF7
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string EncryString(string source)
        {
            source = DesEncryString(source);
            return EncodeBase64(Encoding.UTF7, source);

        }
        /// <summary>
        /// 从UTF7解密成密码，再解密为原始数据
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string DecryString(string source)
        {
            source = DecodeBase64(Encoding.UTF7, source);
            return DesDecryString(source);
        }
        /// <summary>
        /// 从原始数据加密成密码，再加密为UTF7
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string EncryString(string source, string key)
        {
            source = DesEncryString(source, key);
            return EncodeBase64(Encoding.UTF7, source);
        }
        /// <summary>
        /// 从UTF7解密成密码，再解密为原始数据
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DecryString(string source, string key)
        {
            source = DecodeBase64(Encoding.UTF7, source);
            return DesDecryString(source, key);
        }

        public static string Encrypt(string pToEncrypt, string sKey) {
            return DES.Encrypt(pToEncrypt,sKey);
        }

        public static string Decrypt(string pToDecrypt, string sKey) {
            return DES.Decrypt(pToDecrypt, sKey);
        }

    }

    /// <summary>
    /// 有8位长度限制
    /// </summary>
    public sealed class DES
    {
        /// <summary>
        /// 进行DES加密。
        /// </summary>
        /// <param name="pToEncrypt">要加密的字符串。</param>
        /// <param name="sKey">密钥，且必须为8位。</param>
        /// <returns>以Base64格式返回的加密字符串。</returns>
        public static string Encrypt(string pToEncrypt, string sKey)
        {
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Convert.ToBase64String(ms.ToArray());
                ms.Close();
                return str;
            }
        }

        /**/
        /// <summary>
        /// 进行DES解密。
        /// </summary>
        /// <param name="pToDecrypt">要解密的以Base64</param>
        /// <param name="sKey">密钥，且必须为8位。</param>
        /// <returns>已解密的字符串。</returns>
        public static string Decrypt(string pToDecrypt, string sKey)
        {
            byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return str;
            }
        } 

        ///MD5加密
        private static string MD5Encrypt(string sInputString, string sKey)
        {
            //DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            //byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            //des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            //des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            //MemoryStream ms = new MemoryStream();
            //CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            //cs.Write(inputByteArray, 0, inputByteArray.Length);
            //cs.FlushFinalBlock();
            //StringBuilder ret = new StringBuilder();
            //foreach (byte b in ms.ToArray())
            //{
            //    ret.AppendFormat("{0:X2}", b);
            //}
            //ret.ToString();
            //return ret.ToString();

            byte[] data = Encoding.UTF8.GetBytes(sInputString);
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateEncryptor();
            byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
            return BitConverter.ToString(result);


        }

        ///MD5解密
        private static string MD5Decrypt(string sInputString, string sKey)
        {
            //    DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            //    byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            //    for (int x = 0; x < pToDecrypt.Length / 2; x++)
            //    {
            //        int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
            //        inputByteArray[x] = (byte)i;
            //    }

            //    des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            //    des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            //    MemoryStream ms = new MemoryStream();
            //    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            //    cs.Write(inputByteArray, 0, inputByteArray.Length);
            //    cs.FlushFinalBlock();

            //    StringBuilder ret = new StringBuilder();

            //    return System.Text.Encoding.Default.GetString(ms.ToArray());

            string[] sInput = sInputString.Split("-".ToCharArray());
            byte[] data = new byte[sInput.Length];
            for (int i = 0; i < sInput.Length; i++)
            {
                data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
            }
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateDecryptor();
            byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
            return Encoding.UTF8.GetString(result);

        }

        //public static string ComputeHash(string source, string key)
        //{
        //    if (source == null)
        //    {
        //        return "";
        //    }
        //    string str = "abdefghijklmnopqrstuvwxyz";

        //    if (source.Length < 0x1a)
        //    {
        //        source = source + str.Substring(source.Length);
        //    }
        //    byte[] bytes = Encoding.Unicode.GetBytes(source);
        //    int length = bytes.Length;

        //    if (string.IsNullOrEmpty(key))
        //    {
        //        key = _DefaultKey;
        //    }

        //    byte[] buffer2 = Encoding.Unicode.GetBytes(key);
        //    byte num2 = Convert.ToByte(buffer2.Length);
        //    byte num3 = 2;
        //    byte index = 0;
        //    for (int i = 0; i < length; i++)
        //    {
        //        byte[] buffer3;
        //        IntPtr ptr;
        //        byte num5 = (byte)(buffer2[index] | num2);
        //        num5 = (byte)(num5 & num3);
        //        (buffer3 = bytes)[(int)(ptr = (IntPtr)i)] = (byte)(buffer3[(int)ptr] ^ num5);
        //        num3 = (byte)(num3 + 1);
        //        if (num3 > 0xfd)
        //        {
        //            num3 = 2;
        //        }
        //        index = (byte)(index + 1);
        //        if (index >= num2)
        //        {
        //            index = 0;
        //        }
        //    }
        //    return Convert.ToBase64String(bytes, 0, bytes.Length);
        //}

        //public static string SHAComputeHash(string source, string key)
        //{
        //    if (string.IsNullOrEmpty(key))
        //    {
        //        key = _DefaultKey;
        //    }

        //    byte[] bytes = Encoding.UTF8.GetBytes(key);
        //    byte[] buffer = Encoding.Default.GetBytes(source);
        //    string str = "";
        //    if ((buffer.Length > 0) && (buffer[0] != 1))
        //    {
        //        HMACSHA1 hmacsha = new HMACSHA1(bytes);
        //        byte[] inArray = hmacsha.ComputeHash(buffer);
        //        str = Convert.ToString('\x0001') + Convert.ToBase64String(inArray);
        //        hmacsha.Clear();
        //        GC.Collect();
        //        GC.WaitForPendingFinalizers();
        //        return str;
        //    }

        //    return source;
        //}          
    }

    /*
     DES(Data Encryption Standard)和TripleDES是对称加密的两种实现。
    DES和TripleDES基本算法一致，只是TripleDES算法提供的key位数更多，加密可靠性更高。
    DES使用的密钥key为8字节，初始向量IV也是8字节。
    TripleDES使用24字节的key，初始向量IV也是8字节。
    两种算法都是以8字节为一个块进行加密，一个数据块一个数据块的加密，一个8字节的明文加密后的密文也是8字节。如果明文长度不为8字节的整数倍，添加值为0的字节凑满8字节整数倍。所以加密后的密文长度一定为8字节的整数倍。
     */
    //public sealed class DES
    //{
    //    /// <summary>
    //    /// DEC 加密过程
    //    /// </summary>
    //    /// <param name="pToEncrypt">被加密的字符串</param>
    //    /// <param name="sKey">密钥（只支持8个字节的密钥）</param>
    //    /// <returns>加密后的字符串</returns>
    //    public static string Encrypt(string pToEncrypt, string sKey)
    //    {
    //        //访问数据加密标准(DES)算法的加密服务提供程序 (CSP) 版本的包装对象
    //        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    //        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);　//建立加密对象的密钥和偏移量
    //        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);　 //原文使用ASCIIEncoding.ASCII方法的GetBytes方法

    //        byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);//把字符串放到byte数组中

    //        MemoryStream ms = new MemoryStream();//创建其支持存储区为内存的流　
    //        //定义将数据流链接到加密转换的流
    //        CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
    //        cs.Write(inputByteArray, 0, inputByteArray.Length);
    //        cs.FlushFinalBlock();
    //        //上面已经完成了把加密后的结果放到内存中去

    //        StringBuilder ret = new StringBuilder();
    //        foreach (byte b in ms.ToArray())
    //        {
    //            ret.AppendFormat("{0:X2}", b);
    //        }
    //        ret.ToString();
    //        return ret.ToString();
    //    }

    //    /// <summary>
    //    /// DEC 解密过程
    //    /// </summary>
    //    /// <param name="pToDecrypt">被解密的字符串</param>
    //    /// <param name="sKey">密钥（只支持8个字节的密钥，同前面的加密密钥相同）</param>
    //    /// <returns>返回被解密的字符串</returns>
    //    public string Decrypt(string pToDecrypt, string sKey)
    //    {
    //        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

    //        byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
    //        for (int x = 0; x < pToDecrypt.Length / 2; x++)
    //        {
    //            int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
    //            inputByteArray[x] = (byte)i;
    //        }

    //        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);　//建立加密对象的密钥和偏移量，此值重要，不能修改
    //        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    //        MemoryStream ms = new MemoryStream();
    //        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

    //        cs.Write(inputByteArray, 0, inputByteArray.Length);
    //        cs.FlushFinalBlock();

    //        //建立StringBuild对象，createDecrypt使用的是流对象，必须把解密后的文本变成流对象
    //        StringBuilder ret = new StringBuilder();

    //        return System.Text.Encoding.Default.GetString(ms.ToArray());
    //    }
    //}

    public sealed class TripleDES
    {

        public TripleDES()
        {

        }

        //加密，使用密码产生加密算法的公钥，并使用TripleDES对密码进行加密。     
        public static string Encrypt(string pass)
        {
            if (string.IsNullOrEmpty(pass)) return string.Empty;

            try
            {

                byte[] bt = (new System.Text.UnicodeEncoding()).GetBytes(pass);

                PasswordDeriveBytes pdb = new PasswordDeriveBytes(pass, null);

                byte[] key = pdb.GetBytes(24);

                byte[] iv = pdb.GetBytes(8);

                MemoryStream ms = new MemoryStream();

                TripleDESCryptoServiceProvider tdesc = new TripleDESCryptoServiceProvider();

                CryptoStream cs = new CryptoStream(ms, tdesc.CreateEncryptor(key, iv), CryptoStreamMode.Write);

                cs.Write(bt, 0, bt.Length);

                cs.FlushFinalBlock();

                return Convert.ToBase64String(ms.ToArray());

            }

            catch (Exception ex)
            {
                throw ex;
            }

        }


        //解密，使用密码产生加密算法的公钥，并使用TripleDES对加密数据进行解密。     
        public static string Decrypt(string str, string pass)
        {
            if (string.IsNullOrEmpty(str)) return string.Empty;

            try
            {

                byte[] bt = Convert.FromBase64String(str);
                PasswordDeriveBytes pdb = new PasswordDeriveBytes(pass, null);
                byte[] key = pdb.GetBytes(24);
                byte[] iv = pdb.GetBytes(8);

                MemoryStream ms = new MemoryStream();
                TripleDESCryptoServiceProvider tdesc = new TripleDESCryptoServiceProvider();
                CryptoStream cs = new CryptoStream(ms, tdesc.CreateDecryptor(key, iv), CryptoStreamMode.Write);

                cs.Write(bt, 0, bt.Length);
                cs.FlushFinalBlock();
                return (new System.Text.UnicodeEncoding()).GetString(ms.ToArray());
            }

            catch (Exception ex)
            {

                throw ex;

            }

        }

        //使用：     
        //string str = Encrypt("bbb"); 
        //Console.WriteLine(Decrypt(str, "bbb"));    
        //加密，使用密码产生加密算法的公钥，并使用TripleDES对密码进行加密。     
        public static string EncryptWithKey(string pass, string p_key)
        {
            if (string.IsNullOrEmpty(pass)) return string.Empty;

            try
            {

                byte[] bt = (new System.Text.UnicodeEncoding()).GetBytes(pass);

                PasswordDeriveBytes pdb = new PasswordDeriveBytes(p_key, null);

                byte[] key = pdb.GetBytes(24);

                byte[] iv = pdb.GetBytes(8);

                MemoryStream ms = new MemoryStream();

                TripleDESCryptoServiceProvider tdesc = new TripleDESCryptoServiceProvider();

                CryptoStream cs = new CryptoStream(ms, tdesc.CreateEncryptor(key, iv), CryptoStreamMode.Write);

                cs.Write(bt, 0, bt.Length);

                cs.FlushFinalBlock();

                return Convert.ToBase64String(ms.ToArray());

            }

            catch (Exception ex)
            {

                throw ex;

            }

        }

        //解密，使用密码产生加密算法的公钥，并使用TripleDES对加密数据进行解密。     
        public static string DecryptWithKey(string str, string p_key)
        {
            if (string.IsNullOrEmpty(str)) return string.Empty;

            try
            {

                byte[] bt = Convert.FromBase64String(str);

                PasswordDeriveBytes pdb = new PasswordDeriveBytes(p_key, null);

                byte[] key = pdb.GetBytes(24);

                byte[] iv = pdb.GetBytes(8);

                MemoryStream ms = new MemoryStream();

                TripleDESCryptoServiceProvider tdesc = new TripleDESCryptoServiceProvider();

                CryptoStream cs = new CryptoStream(ms, tdesc.CreateDecryptor(key, iv), CryptoStreamMode.Write);

                cs.Write(bt, 0, bt.Length);

                cs.FlushFinalBlock();

                return (new System.Text.UnicodeEncoding()).GetString(ms.ToArray());

            }

            catch (Exception ex)
            {

                throw ex;

            }

        }
    }

    public sealed class JhEncrypt
    {
        private static readonly string _DefaultKey = "qqq";
        /// <summary> 
        /// 使用缺省密钥字符串加密 
        /// </summary> 
        /// <param name="original">明文</param> 
        /// <returns>密文</returns> 
        public static string Encrypt(string original)
        {
            return Encrypt(original, _DefaultKey);
        }
        /// <summary> 
        /// 使用缺省密钥解密 
        /// </summary> 
        /// <param name="original">密文</param> 
        /// <returns>明文</returns> 
        public static string Decrypt(string original)
        {
            return Decrypt(original, _DefaultKey, System.Text.Encoding.Default);
        }
        /// <summary> 
        /// 使用给定密钥解密 
        /// </summary> 
        /// <param name="original">密文</param> 
        /// <param name="key">密钥</param> 
        /// <returns>明文</returns> 
        public static string Decrypt(string original, string key)
        {
            return Decrypt(original, key, System.Text.Encoding.Default);
        }
        /// <summary> 
        /// 使用缺省密钥解密,返回指定编码方式明文 
        /// </summary> 
        /// <param name="original">密文</param> 
        /// <param name="encoding">编码方式</param> 
        /// <returns>明文</returns> 
        public static string Decrypt(string original, Encoding encoding)
        {
            return Decrypt(original, _DefaultKey, encoding);
        }
        /// <summary> 
        /// 使用给定密钥加密 
        /// </summary> 
        /// <param name="original">原始文字</param> 
        /// <param name="key">密钥</param> 
        /// <param name="encoding">字符编码方案</param> 
        /// <returns>密文</returns> 
        public static string Encrypt(string original, string key)
        {
            byte[] buff = System.Text.Encoding.Default.GetBytes(original);
            byte[] kb = System.Text.Encoding.Default.GetBytes(key);
            return Convert.ToBase64String(Encrypt(buff, kb));
        }

        /// <summary> 
        /// 使用给定密钥解密 
        /// </summary> 
        /// <param name="encrypted">密文</param> 
        /// <param name="key">密钥</param> 
        /// <param name="encoding">字符编码方案</param> 
        /// <returns>明文</returns> 
        public static string Decrypt(string encrypted, string key, Encoding encoding)
        {
            byte[] buff = Convert.FromBase64String(encrypted);
            byte[] kb = System.Text.Encoding.Default.GetBytes(key);
            return encoding.GetString(Decrypt(buff, kb));
        }
        /// <summary> 
        /// 生成MD5摘要 
        /// </summary> 
        /// <param name="original">数据源</param> 
        /// <returns>摘要</returns> 
        public static byte[] MakeMD5(byte[] original)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            byte[] keyhash = hashmd5.ComputeHash(original);
            hashmd5 = null;
            return keyhash;
        }

        /// <summary> 
        /// 使用给定密钥加密 
        /// </summary> 
        /// <param name="original">明文</param> 
        /// <param name="key">密钥</param> 
        /// <returns>密文</returns> 
        public static byte[] Encrypt(byte[] original, byte[] key)
        {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            des.Key = MakeMD5(key);
            des.Mode = CipherMode.ECB;

            return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
        }

        /// <summary> 
        /// 使用给定密钥解密数据 
        /// </summary> 
        /// <param name="encrypted">密文</param> 
        /// <param name="key">密钥</param> 
        /// <returns>明文</returns> 
        public static byte[] Decrypt(byte[] encrypted, byte[] key)
        {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            des.Key = MakeMD5(key);
            des.Mode = CipherMode.ECB;

            return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
        }

        /// <summary> 
        /// 使用给定密钥加密 
        /// </summary> 
        /// <param name="original">原始数据</param> 
        /// <param name="key">密钥</param> 
        /// <returns>密文</returns> 
        public static byte[] Encrypt(byte[] original)
        {
            byte[] key = System.Text.Encoding.Default.GetBytes(_DefaultKey);
            return Encrypt(original, key);
        }

        /// <summary> 
        /// 使用缺省密钥解密数据 
        /// </summary> 
        /// <param name="encrypted">密文</param> 
        /// <param name="key">密钥</param> 
        /// <returns>明文</returns> 
        public static byte[] Decrypt(byte[] encrypted)
        {
            byte[] key = System.Text.Encoding.Default.GetBytes(_DefaultKey);
            return Decrypt(encrypted, key);
        }

    }


    public sealed class TripleDESHelper
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="toEncrypt">要加密的字符串，即明文</param>
        /// <param name="key">公共密钥</param>
        /// <param name="useHashing">是否使用MD5生成机密秘钥</param>
        /// <returns>加密后的字符串，即密文</returns>
        public static string Encrypt(string toEncrypt, string key, bool useHashing)
        {
            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

                if (useHashing)
                {
                    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                }
                else
                    keyArray = UTF8Encoding.UTF8.GetBytes(key);

                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

                tdes.Key = keyArray;
                tdes.Mode = CipherMode.ECB;
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = tdes.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch
            {

            }
            return string.Empty;
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="toDecrypt">要解密的字符串，即密文</param>
        /// <param name="key">公共密钥</param>
        /// <param name="useHashing">是否使用MD5生成机密密钥</param>
        /// <returns>解密后的字符串，即明文</returns>
        public static string Decrypt(string toDecrypt, string key, bool useHashing)
        {
            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

                if (useHashing)
                {
                    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                }
                else
                    keyArray = UTF8Encoding.UTF8.GetBytes(key);

                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                tdes.Key = keyArray;
                tdes.Mode = CipherMode.ECB;
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = tdes.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                return UTF8Encoding.UTF8.GetString(resultArray);

            }
            catch
            {

            }
            return string.Empty;

        }
    }
}
